/**
 * Created by Melvin on 15/04/15.
 */
(function () {
    'use strict';

    angular.module('app.ejercicios')
        .controller('ListaEjerciciosCtrl', ['$scope', '$filter', 'EjerciciosService', ListaEjerciciosCtrl])



    function ListaEjerciciosCtrl ($scope, $filter, EjerciciosService){
        var init;

        $scope.listOfItems = [];

        $scope.searchKeywords = '';

        $scope.filteredStores = [];

        $scope.row = '';

        $scope.select = function(page) {
            var end, start;
            start = (page - 1) * $scope.numPerPage;
            end = start + $scope.numPerPage;
            return $scope.currentPageItems = $scope.filteredStores.slice(start, end);
        };

        $scope.onFilterChange = function() {
            $scope.select(1);
            $scope.currentPage = 1;
            return $scope.row = '';
        };

        $scope.onNumPerPageChange = function() {
            $scope.select(1);
            return $scope.currentPage = 1;
        };

        $scope.onOrderChange = function() {
            $scope.select(1);
            return $scope.currentPage = 1;
        };

        $scope.search = function() {
            $scope.filteredStores = $filter('filter')($scope.listOfItems, $scope.searchKeywords);
            return $scope.onFilterChange();
        };

        $scope.order = function(rowName) {
            if ($scope.row === rowName) {
                return;
            }
            $scope.row = rowName;
            $scope.filteredStores = $filter('orderBy')($scope.listOfItems, rowName);
            return $scope.onOrderChange();
        };

        $scope.numPerPageOpt = [3, 5, 10, 20];

        $scope.numPerPage = $scope.numPerPageOpt[2];

        $scope.currentPage = 1;

        $scope.currentPageItems = [];

        init = function() {
            $scope.search();
            return $scope.select($scope.currentPage);
        };

        $scope.ListarE=function() {
            EjerciciosService.Listar()
                .success(function (response) {
                    $scope.listOfItems = angular.fromJson(response);
                    init();
                }).error(function () {
                    console.log(arguments)
                });
        }
        $scope.ListarE();
/*
        $scope.Eliminar= function(idMusculo) {
            alert(idMusculo);
            EjerciciosService.Eliminar(idMusculo)
                .success(function (response) {
                    $scope.ListarM();
                }).error(function () {
                    console.log(arguments);
                    alert("Error");
                });
        }
*/



        init();
    }

    function EditarMusculoCtrl ($scope, $stateParams, MusculosService){
        $scope.musculo = {};


        $scope.guardarInfo = function () {
            $scope.musculo.Id=$stateParams.id;
            MusculosService.Modificar($scope.musculo)
                .success(function (response) {
                  //  alert("se modifico");
                }).error(function () {
                    console.log(arguments);
                  //  alert(arguments);
                });
        };


    }



})();
